package com.cqut.hfh.ordering.controller;

import com.cqut.hfh.ordering.service.IFodIndentService;
import com.cqut.hfh.ordering.utils.EntityIdutils;
import com.cqut.hfh.ordering.utils.Message;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author LJH
 * @date 2019/6/25-14:22
 */
@RequestMapping("/fodIndent")
@Controller
public class FodIndentController {

    @Resource
    private IFodIndentService fodIndentService;


    /**
     * 返回所有订单
     * @return
     */
    @RequestMapping(path = "/findAll", produces = {"text/html;charset=utf-8"})
    @ResponseBody
    public String findAll(@RequestParam("limit") String limit,
                          @RequestParam("page") String page){
        List<Map<String, Object>> result_list = fodIndentService.getFodIndents(limit, page);

        for( Map<String, Object> map : result_list){
            if( map.get("isva").toString().equals("1")){
                map.put("isva", "有效");
            }
            if( map.get("isva").toString().equals("0")){
                map.put("isva", "无效");
            }
            DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String tsStr = sdf.format(map.get("optime"));
            map.put("optime", tsStr);
        }

        Integer count = fodIndentService.getCount();

        return Message.getReturnMsg(0,"", count, result_list);
    }


    /**
     * 删除指定ID订单
     * @return
     */
    @RequestMapping(path = "/updateById"/*, produces = {"text/html;charset=utf-8"}*/)
    @ResponseBody
    public String updateById(@RequestParam("indentId")String indent_id){
        int a = fodIndentService.updateById(indent_id);
        return Message.getReturnMsg(0,"ok");
    }



    /**
     * 根据订单号查订单
     * @param limit
     * @param page
     * @return
     */
    @RequestMapping(path = "/findFodIndentsByName", produces = {"text/html;charset=utf-8"})
    @ResponseBody
    public String findFodIndentsByName(@RequestParam("limit") String limit,
                                    @RequestParam("page") String page,
                                    @RequestParam("indent_name") String indent_id){

        List<Map<String, Object>> indent_list = fodIndentService.getFodIndentsByName(limit, page, indent_id);

        for( Map<String, Object> map : indent_list){
            if( map.get("isva").toString().equals("1")){
                map.put("isva", "有效");
            }
            if( map.get("isva").toString().equals("0")){
                map.put("isva", "无效");
            }
            DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String tsStr = sdf.format(map.get("optime"));
            map.put("optime", tsStr);
        }

        Integer count = indent_list.size();

        return Message.getReturnMsg(0,"", count, indent_list);
    }

    /**
     * 根据订单ID更新派送状态
     * @param indent_id
     * @return
     */
    @RequestMapping(path = "/updatePsById", produces = {"text/html;charset=utf-8"})
    @ResponseBody
    public String updatePsById(@RequestParam("indentId") String indent_id){
        int a = fodIndentService.updatePsById(indent_id);

        return Message.getReturnMsg(0,"ok");
    }



//    /**
//     * 添加更新并订单
//     * @param module
//     * @return
//     */
//    @RequestMapping(path = "/saveOrUpdate", produces = {"text/html;charset=utf-8"})
//    @ResponseBody
//    public String saveOrUpdate(FodIndent module){
//
//        module.setOptime(new Date());
//
//        if( module.getMod_id() == null){
//            module.setMod_id(EntityIdutils.getId());
//            int temp = fodIndentService.saveFodIndent(module);
//            return Message.getReturnMsg(0,"ok");
//        }else{
//            int temp = fodIndentService.updateFodIndent(module);
//        }
//        return Message.getReturnMsg(0,"ok");
//    }
//
//    /**
//     *  根据模板ID查询信息
//     * @param mod_id
//     * @return
//     */
//    @PostMapping("/findById")
//    @ResponseBody
//    public FodIndent findById(@RequestParam("id")String mod_id){
//        return fodIndentService.findById(mod_id);
//    }

}
